package com.woc.cloud.api.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woc.cloud.api.constant.ErrorCode;
import com.woc.cloud.api.entity.Response;
import com.woc.cloud.srv.model.WocUsers;
import com.woc.cloud.srv.service.WocUsersService;

/**
 * 用户管理控制器
 * 
 * @author     woc
 * @date       2024-01-26 
 * @version    0.0.1
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private WocUsersService wocUsersService;

    @RequestMapping(value = {"/list"}, method = RequestMethod.GET)
    public Object list(@RequestParam(required = false, defaultValue = "0") Integer offset,
            @RequestParam(required = false, defaultValue = "10") Integer limit) {
        QueryWrapper<WocUsers> wrapper = new QueryWrapper<>();
        Map<String, Object> result = new HashMap<>();
        result.put("total", wocUsersService.count(wrapper));
        wrapper.orderByDesc("uid");
        wrapper.last(" limit " + offset + "," + limit);
        result.put("rows", wocUsersService.list(wrapper));
        return result;
    }

    @RequestMapping(value = "/info", method = RequestMethod.GET)
    @ResponseBody
    public Response<Object> info(Integer uid) {
    	Response<Object> resp = new Response<>();
        if (uid == null || uid <= 0) {
            return resp.withError(ErrorCode.ParamsError);
        }
        WocUsers user = wocUsersService.getById(uid);
        if (user == null) {
            return resp.withError("用户信息查询失败");
        }
        return resp.withOk(user);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public Response<Object> save(@Validated @RequestBody WocUsers user) {
        Response<Object> resp = new Response<>();
        if (user == null || StringUtils.isBlank(user.getName()) || StringUtils.isBlank(user.getPass())) {
            return resp.withError(ErrorCode.ParamsError);
        }
        user.setPass(DigestUtils.md5DigestAsHex(user.getPass().getBytes()));
        QueryWrapper<WocUsers> wrapper = new QueryWrapper<>();
        wrapper.eq("name", user.getName());
        WocUsers exist = wocUsersService.getOne(wrapper);
        if (user.getUid() == null) {
            if (exist != null) {
                return resp.withError("用户信息不能重复");
            }
            user.setCreated(0);
            user.setAccess(0);
            user.setLogin(0);
            user.setIp("");
            if (!wocUsersService.save(user)) {
                return resp.withError("用户信息新增失败");
            }
        } else {
            if (exist != null && !exist.getUid().equals(user.getUid())) {
                return resp.withError("用户信息不能重复");
            }
            if (!wocUsersService.updateById(user)) {
                return resp.withError("用户信息更新失败");
            }
        }
        return resp.withOk();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Response<Object> delete(Integer uid) {
        Response<Object> resp = new Response<>();
        if (uid == null || uid <= 0) {
            return resp.withError(ErrorCode.ParamsError);
        }
        if (!wocUsersService.removeById(uid)) {
            return resp.withError("用户信息删除失败");
        }
        return resp.withOk();
    }

}
